package com.iotechn.unimall.admin.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.biz.service.goods.GoodsBizService;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.core.exception.ServiceExceptionDefinition;
import com.iotechn.unimall.core.util.MoneyUtils;
import com.iotechn.unimall.data.component.LockComponent;
import com.iotechn.unimall.data.domain.*;
import com.iotechn.unimall.data.dto.AdminDTO;
import com.iotechn.unimall.data.dto.warehouse.SalesSendOrderDTO;
import com.iotechn.unimall.data.dto.warehouse.SalesSendOrderGoodsDTO;
import com.iotechn.unimall.data.dto.warehouse.SalesSendOrderOperLogDTO;
import com.iotechn.unimall.data.enums.SpuStatusType;
import com.iotechn.unimall.data.enums.SystemStatusType;
import com.iotechn.unimall.data.enums.salesSendOrder.SalesSendOrderStateType;
import com.iotechn.unimall.data.mapper.*;
import com.iotechn.unimall.data.model.Page;
import com.iotechn.unimall.data.util.SessionUtil;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


@Service
public class SalesSendOrderServiceImpl implements SalesSendOrderService {

    private static final String STORE_NO_PRE = "CK";
    private static final Logger logger = LoggerFactory.getLogger(SalesSendOrderServiceImpl.class);
    @Autowired
    private SalesSendOrderMapper salesSendOrderMapper;
    @Autowired
    private SalesSendOrderGoodsMapper salesSendOrderGoodsMapper;
    @Autowired
    private SalesSendOperLogMapper salesSendOperLogMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private WarehouseGoodsMapper warehouseGoodsMapper;
    @Autowired
    private WarehouseOperLogMapper warehouseOperLogMapper;
    @Autowired
    private AccountsReceivableMapper accountsReceivableMapper;
    @Autowired
    private GoodsBizService goodsBizService;
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private StoreMapper storeMapper;

    @Override
    public Page<SalesSendOrderDO> list(Integer page, Integer limit, Long adminId, String sendOrderNo, String skuSearch, String insertStartTime, String insertEndTime,Long storeId, String sendOrderState) throws ServiceException {
        Wrapper<SalesSendOrderDO> wrapper = new EntityWrapper<SalesSendOrderDO>();
        if (!StringUtils.isEmpty(sendOrderNo)) {
            wrapper.like("send_order_no", sendOrderNo);
        }
        if(storeId != null) {
            wrapper.eq("store_id", storeId);
        }
        if (!StringUtils.isEmpty(sendOrderState)) {
            wrapper.eq("send_order_state", sendOrderState);
        }
        //时间范围搜索
        if (!StringUtils.isEmpty(insertStartTime)) {
            wrapper.gt("gmt_create", insertStartTime);
        }
        if (!StringUtils.isEmpty(insertEndTime)) {
            wrapper.le("gmt_create", insertEndTime);
        }
        List<Long> orderIds = new LinkedList<>();
        if (!StringUtils.isEmpty(skuSearch)) {
            List<SalesSendOrderGoodsDO> orderSkus = salesSendOrderGoodsMapper.selectList(new EntityWrapper<SalesSendOrderGoodsDO>()
                    .like("goods_bar_code", skuSearch).or().like("spu_name", skuSearch).or().like("sku_name", skuSearch));

            if (CollectionUtils.isEmpty(orderSkus)) {
                return new Page<>(new ArrayList<>(), page, limit, 0L);
            }
            orderIds = orderSkus.stream().map(SalesSendOrderGoodsDO::getSendOrderId).distinct().collect(Collectors.toList());
            wrapper.in(!CollectionUtils.isEmpty(orderIds), "id", orderIds);
        }
        wrapper.orderBy("id", false);
        Integer count = salesSendOrderMapper.selectCount(wrapper);
        List<SalesSendOrderDO> SalesSendOrderDOS = salesSendOrderMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        SalesSendOrderDOS.forEach(item -> {
            List<SalesSendOrderGoodsDO> skus = salesSendOrderGoodsMapper.selectList(new EntityWrapper<SalesSendOrderGoodsDO>()
                    .eq("send_order_id", item.getId()));
            item.setSkus(skus);

        });
        return new Page<>(SalesSendOrderDOS, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(SalesSendOrderDTO salesSendOrderDTO, Long adminId) throws ServiceException {
        List<SkuDO> skuDOList = skuMapper.selectList(new EntityWrapper<SkuDO>().eq("status", SystemStatusType.ON.getCode()));
        Map<Long, SkuDO> skuDOMap = skuDOList.stream().collect(Collectors.toMap(SkuDO::getId, Function.identity(), (key1, key2) -> key2));
        //参数校验
        if (CollectionUtils.isEmpty(salesSendOrderDTO.getSalesSendOrderGoodsDTOS())) {
            //未选择入库商品
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        Long storeId = salesSendOrderDTO.getStoreId();
        StoreDO storeDO = storeMapper.selectById(storeId);
        Date now = new Date();
        SalesSendOrderDO salesSendOrderDO = new SalesSendOrderDO();
        BeanUtils.copyProperties(salesSendOrderDTO, salesSendOrderDO);
        salesSendOrderDO.setGmtUpdate(now);
        salesSendOrderDO.setGmtCreate(now);
        salesSendOrderDO.setAdminId(adminId);
        salesSendOrderDO.setStoreType(storeDO.getStoreType());
        salesSendOrderDO.setSendOrderState(0);//待出库
        salesSendOrderDO.setSendOrderConpon(salesSendOrderDTO.getSendOrderConpon() == null ? 0 : salesSendOrderDTO.getSendOrderConpon() * 100);
        salesSendOrderDO.setSendOrderNo("CK" + System.currentTimeMillis());//出库单编号自动生成
        salesSendOrderDO.setSendOrderGoodsPrice((int) (salesSendOrderDTO.getTotalPrice() * 100));
        salesSendOrderDO.setSendOrderGoodsCount(salesSendOrderDTO.getTotal());
        Float sendOrderAmount = salesSendOrderDTO.getTotalPrice() * 100 - (salesSendOrderDTO.getSendOrderConpon() == null ? 0 : salesSendOrderDTO.getSendOrderConpon() * 100);
        salesSendOrderDO.setSendOrderPrice(sendOrderAmount.intValue());
        //操作人真实姓名
        AdminDTO adminDTO = SessionUtil.getAdmin();
        if (adminDTO != null) {
            salesSendOrderDO.setAdminRealname(adminDTO.getRealname());
        }
        if (salesSendOrderMapper.insert(salesSendOrderDO) > 0) {

            //插入出库商品表
            for (SalesSendOrderGoodsDTO sendOrderGoodsDTO : salesSendOrderDTO.getSalesSendOrderGoodsDTOS()) {
                if(sendOrderGoodsDTO.getSendGoodsStock() != null && sendOrderGoodsDTO.getSendGoodsPrice() != null){
                    Double mul = MoneyUtils.mul(sendOrderGoodsDTO.getSendGoodsStock().doubleValue(), sendOrderGoodsDTO.getSendGoodsPrice().doubleValue());
                    Double allPrice = MoneyUtils.div(mul, 1D, 2);
                    sendOrderGoodsDTO.setSendGoodsAllPrice(allPrice.floatValue());
                }
                if(sendOrderGoodsDTO.getSendGoodsAllPrice() == null || sendOrderGoodsDTO.getSendGoodsStock() == null ||
                        sendOrderGoodsDTO.getSendGoodsAllPrice().floatValue()  == 0.0F || sendOrderGoodsDTO.getSendGoodsStock().floatValue() == 0.0F ){
                    throw new AdminServiceException(new ServiceExceptionDefinition(500, "请确认" + sendOrderGoodsDTO.getGoodsName()+"的商品出库信息是否完整"));
                }
                sendOrderGoodsDTO.setSendGoodsAllPrice(sendOrderGoodsDTO.getSendGoodsAllPrice() * 100);
                sendOrderGoodsDTO.setSendGoodsStock(sendOrderGoodsDTO.getSendGoodsStock());
                SalesSendOrderGoodsDO salesSendOrderGoodsDO = new SalesSendOrderGoodsDO();
                salesSendOrderGoodsDO.setSkuId(sendOrderGoodsDTO.getSkuId());
                salesSendOrderGoodsDO.setWarehouseId(salesSendOrderDO.getWarehouseId());
                salesSendOrderGoodsDO.setSendGoodsStock(sendOrderGoodsDTO.getSendGoodsStock());
                Double sendGoodsPrice = MoneyUtils.div(sendOrderGoodsDTO.getSendGoodsAllPrice().doubleValue(), sendOrderGoodsDTO.getSendGoodsStock().doubleValue(), 2);
                salesSendOrderGoodsDO.setSendGoodsPrice(sendGoodsPrice.longValue());
                salesSendOrderGoodsDO.setSendGoodsAmount(sendOrderGoodsDTO.getSendGoodsAllPrice());
                salesSendOrderGoodsDO.setSendGoodsTax(sendOrderGoodsDTO.getSendGoodsTax());
                salesSendOrderGoodsDO.setSendOrderId(salesSendOrderDO.getId());
                salesSendOrderGoodsDO.setAdminId(adminId);
                salesSendOrderGoodsDO.setGmtCreate(now);
                salesSendOrderGoodsDO.setGmtUpdate(now);
                salesSendOrderGoodsDO.setGoodsImg(sendOrderGoodsDTO.getImg());
                salesSendOrderGoodsDO.setGoodsBarCode(sendOrderGoodsDTO.getGoodsBarCode());
                if(org.apache.commons.lang3.StringUtils.isEmpty(sendOrderGoodsDTO.getGoodsBarCode())){
                    SkuDO skuDO = skuDOMap.get(sendOrderGoodsDTO.getSkuId());
                    if(skuDO != null){
                        salesSendOrderGoodsDO.setGoodsBarCode(skuDO.getBarCode());
                    }
                }
                salesSendOrderGoodsDO.setGoodsUnit(sendOrderGoodsDTO.getGoodsUnit());
                salesSendOrderGoodsDO.setGoodsId(sendOrderGoodsDTO.getGoodsId());
                salesSendOrderGoodsDO.setGoodsName(sendOrderGoodsDTO.getGoodsName());
                salesSendOrderGoodsDO.setSkuName(sendOrderGoodsDTO.getSkuName());
                salesSendOrderGoodsDO.setSpuName(sendOrderGoodsDTO.getSpuName());
                if(salesSendOrderGoodsDO.getSendGoodsPrice() == null){
                    throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
                }
                salesSendOrderGoodsMapper.insert(salesSendOrderGoodsDO);
            }
            //插入操作表
            //操作人真实姓名
            if (adminDTO != null) {
                salesSendOrderDO.setAdminRealname(adminDTO.getRealname());
                insertSendOrderOperLog(salesSendOrderDO, adminDTO);
            }
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(SalesSendOrderDTO salesSendOrderDTO, Long adminId) throws ServiceException {
        Long storeId = salesSendOrderDTO.getStoreId();
        StoreDO storeDO = storeMapper.selectById(storeId);
        Long id = salesSendOrderDTO.getId();
        if (id == null) {
            return "fail";
        }
        Date now = new Date();
        SalesSendOrderDO salesSendOrderDO = new SalesSendOrderDO();
        BeanUtils.copyProperties(salesSendOrderDTO, salesSendOrderDO);
        salesSendOrderDO.setGmtUpdate(now);
        salesSendOrderDO.setAdminId(adminId);
        salesSendOrderDO.setStoreType(storeDO.getStoreType());
        salesSendOrderDO.setSendOrderConpon(salesSendOrderDTO.getSendOrderConpon() == null ? 0 : salesSendOrderDTO.getSendOrderConpon() * 100);
        salesSendOrderDO.setSendOrderGoodsPrice((int) (salesSendOrderDTO.getTotalPrice() * 100));
        salesSendOrderDO.setSendOrderGoodsCount(salesSendOrderDTO.getTotal());
        Float sendOrderAmount = salesSendOrderDTO.getTotalPrice() * 100 - (salesSendOrderDTO.getSendOrderConpon() == null ? 0 : salesSendOrderDTO.getSendOrderConpon() * 100);
        salesSendOrderDO.setSendOrderPrice(sendOrderAmount.intValue());
        //删除出库单-商品数据
        salesSendOrderGoodsMapper.delete(new EntityWrapper<SalesSendOrderGoodsDO>()
                .eq("send_order_id", salesSendOrderDTO.getId())
                .eq("warehouse_id", salesSendOrderDTO.getWarehouseId()));
        salesSendOrderDO.setGmtUpdate(now);
        if (salesSendOrderMapper.updateById(salesSendOrderDO) > 0) {
            for (SalesSendOrderGoodsDTO sendOrderGoodsDTO : salesSendOrderDTO.getSalesSendOrderGoodsDTOS()) {
                SalesSendOrderGoodsDO salesSendOrderGoodsDO = new SalesSendOrderGoodsDO();
                BeanUtils.copyProperties(sendOrderGoodsDTO, salesSendOrderGoodsDO);
                salesSendOrderGoodsDO.setSendOrderId(salesSendOrderDO.getId());
                salesSendOrderGoodsDO.setGoodsBarCode(sendOrderGoodsDTO.getBarCode());
                salesSendOrderGoodsDO.setWarehouseId(salesSendOrderDO.getWarehouseId());
                salesSendOrderGoodsDO.setGoodsImg(sendOrderGoodsDTO.getImg());
                salesSendOrderGoodsDO.setGoodsUnit(sendOrderGoodsDTO.getUnit());
                salesSendOrderGoodsDO.setSendGoodsPrice((long) (sendOrderGoodsDTO.getSendGoodsAllPrice() / sendOrderGoodsDTO.getSendGoodsStock() * 100));
                salesSendOrderGoodsDO.setSendGoodsAmount(0.0F);
                if(sendOrderGoodsDTO.getSendGoodsAllPrice() != null){
                    salesSendOrderGoodsDO.setSendGoodsAmount(sendOrderGoodsDTO.getSendGoodsAllPrice() * 100F);
                }
                salesSendOrderGoodsDO.setGmtCreate(now);
                salesSendOrderGoodsDO.setGmtUpdate(now);
                salesSendOrderGoodsDO.setAdminId(adminId);
                salesSendOrderGoodsMapper.insert(salesSendOrderGoodsDO);
            }
            //操作人真实姓名
            AdminDTO adminDTO = SessionUtil.getAdmin();
            if (adminDTO != null) {
                salesSendOrderDO.setAdminRealname(adminDTO.getRealname());
                insertSendOrderOperLog(salesSendOrderDO, adminDTO);
            }
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (salesSendOrderMapper.deleteById(id) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);

    }

    @Override
    public SalesSendOrderDTO detail(Long id, Long adminId) throws ServiceException {
        SalesSendOrderDO salesSendOrderDO = salesSendOrderMapper.selectById(id);
        SalesSendOrderDTO salesSendOrderDTO = new SalesSendOrderDTO();
        BeanUtils.copyProperties(salesSendOrderDO, salesSendOrderDTO);
        WarehouseDO warehouseDO = warehouseMapper.selectById(salesSendOrderDO.getWarehouseId());
        if (warehouseDO != null) {
            salesSendOrderDTO.setWarehouseName(warehouseDO.getWarehouseName());
        }
        //处理价格
        salesSendOrderDTO.setSendOrderGoodsPrice(Float.valueOf(salesSendOrderDO.getSendOrderPrice()) / 100);
        salesSendOrderDTO.setSendOrderConpon(salesSendOrderDO.getSendOrderConpon() == null ? 0 : salesSendOrderDO.getSendOrderConpon() / 100);
//通过出库单获取商品
        List<SalesSendOrderGoodsDTO> sendOrderGoodsDTOS = salesSendOrderGoodsMapper.querySkusByPara(null, id, null, null, null);
        //处理价格
        sendOrderGoodsDTOS.stream().map(i -> {
            Float originalGoodPrice = Float.valueOf(String.valueOf(i.getSendGoodsPrice()));
            i.setSendGoodsPrice(originalGoodPrice / 100);
            return i;
        }).collect(Collectors.toList());
        salesSendOrderDTO.setSalesSendOrderGoodsDTOS(sendOrderGoodsDTOS);
        //操作日志
        List<SalesSendOrderOperLogDTO> salesSendOrderOperLogDTOS = salesSendOperLogMapper.listSalesSendOrderLogsByPara(id);
        salesSendOrderDTO.setSalesSendOrderOperLogDTOS(salesSendOrderOperLogDTOS);
        return salesSendOrderDTO;
    }

    @Autowired
    private LockComponent lockComponent;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String audit(Long salesSendOrderId, Integer sendOrderState, String auditComment, Long storePurchaseOrderId, Long storeStockEntryId, Long adminId) throws ServiceException {
        if (salesSendOrderId == null || "".equals(salesSendOrderId)) {
            throw new AdminServiceException(new ServiceExceptionDefinition(500, "缺少出库单id"));
        }
        SalesSendOrderDO salesSendOrderDO = salesSendOrderMapper.selectById(salesSendOrderId);
        return salesSendOrderAudio(salesSendOrderDO,sendOrderState,auditComment,storePurchaseOrderId,storeStockEntryId,adminId);
    }




    /**
     * 插入操作日志 仓库-出库单
     *
     * @throws Exception
     */
    private void insertSendOrderOperLog(SalesSendOrderDO salesSendOrderDO, AdminDTO adminDTO) {
        Date now = new Date();
        SalesSendOperLogDO salesSendOperLogDO = new SalesSendOperLogDO();
        salesSendOperLogDO.setSendOrderId(salesSendOrderDO.getId());
        salesSendOperLogDO.setGmtCreate(now);
        salesSendOperLogDO.setOperTime(now);
        salesSendOperLogDO.setOperUserId(adminDTO.getId());
        salesSendOperLogDO.setOperUser(adminDTO.getRealname());
        salesSendOperLogDO.setOperUserUsername(adminDTO.getUsername());
        salesSendOperLogDO.setOrderState(salesSendOrderDO.getSendOrderState());
        salesSendOperLogMapper.insert(salesSendOperLogDO);
    }


    public String salesSendOrderAudio(SalesSendOrderDO salesSendOrderDO, Integer sendOrderState, String auditComment, Long storePurchaseOrderId, Long storeStockEntryId, Long adminId) throws ServiceException {
        if (salesSendOrderDO == null) {
            throw new AdminServiceException(new ServiceExceptionDefinition(500, "出库单不存在"));
        }
        if (lockComponent.tryLock("ck"+salesSendOrderDO.getSendOrderNo(), 6)) {
            if (salesSendOrderDO.getSendOrderState().intValue() == SalesSendOrderStateType.ALREADY.getCode()) {
                throw new AdminServiceException(new ServiceExceptionDefinition(500, "当前出库单已审核"));
            }
        }
        WarehouseDO warehouseDO = warehouseMapper.selectById(salesSendOrderDO.getWarehouseId());
        salesSendOrderDO.setSendOrderState(sendOrderState);
        salesSendOrderDO.setCheckComment(auditComment);
        Date now = new Date();
        AdminDTO adminDTO = SessionUtil.getAdmin();
        boolean isPassed = salesSendOrderDO.getSendOrderState().intValue() == 1;
        if (adminDTO != null && isPassed) {
            salesSendOrderDO.setSendOrderConfirmUserId(adminId);
            salesSendOrderDO.setSendOrderConfirmRealname(adminDTO.getRealname());
            salesSendOrderDO.setSendOutTime(now);
            if (salesSendOrderDO.getCheckId() == null) {
                salesSendOrderDO.setCheckId(adminId);
                salesSendOrderDO.setCheckTime(now);
                salesSendOrderDO.setCheckUserRealname(adminDTO.getRealname());
            }
            if(salesSendOrderDO.getAdminId() == null || salesSendOrderDO.getAdminId().longValue() ==0){
                salesSendOrderDO.setAdminId(adminDTO.getId());
                salesSendOrderDO.setAdminRealname(adminDTO.getRealname());
            }
        }
        //修改仓库出库单
        if (salesSendOrderMapper.updateById(salesSendOrderDO) > 0) {
            if (isPassed) {
                List<SalesSendOrderGoodsDO> salesSendOrderGoodsDOS = salesSendOrderGoodsMapper.selectList(new EntityWrapper<SalesSendOrderGoodsDO>()
                        .eq("send_order_id", salesSendOrderDO.getId())
                        .eq("warehouse_id", salesSendOrderDO.getWarehouseId()));
                //添加门店要货订单
                for (SalesSendOrderGoodsDO salesSendOrderGoodsDO : salesSendOrderGoodsDOS) {
                    //更新库存商品数量
                    List<WarehouseGoodsDO> warehouseGoodsDOS = warehouseGoodsMapper.selectList(new EntityWrapper<WarehouseGoodsDO>()
                            .eq("warehouse_id", salesSendOrderDO.getWarehouseId())
                            .eq("sku_id", salesSendOrderGoodsDO.getSkuId()));
                    WarehouseGoodsDO warehouseGoodsDO = null;
                    WarehouseGoodsDO origWarehouseGoodsDO = null;
                    Float changeSkuStock = salesSendOrderGoodsDO.getSendGoodsStock();
                    Float totalSkuStock = 0.0f;
                    if (!CollectionUtils.isEmpty(warehouseGoodsDOS)) {
                        warehouseGoodsDO = warehouseGoodsDOS.get(0);
                        origWarehouseGoodsDO = new WarehouseGoodsDO();
                        BeanUtils.copyProperties(warehouseGoodsDO,origWarehouseGoodsDO);
                        if (warehouseGoodsDO != null) {
                            Float warehouseSkuStock = warehouseGoodsDO.getWarehouseSkuStock();
                            warehouseSkuStock = warehouseSkuStock == null || "".equals(warehouseGoodsDO) ? 0 : warehouseSkuStock;
                            totalSkuStock = warehouseSkuStock - changeSkuStock;
                            if (totalSkuStock >= 0) {
                                warehouseGoodsDO.setWarehouseSkuStock(totalSkuStock);
                                warehouseGoodsDO.setGmtUpdate(now);
                                warehouseGoodsDO.setWarehouseName(warehouseDO.getWarehouseName());
                                warehouseGoodsMapper.updateById(warehouseGoodsDO);
                            } else {
                                logger.error("skuId:{} 库存不足,原有stock:{},现有stock:{}", warehouseGoodsDO.getSkuId(), warehouseGoodsDO.getWarehouseSkuStock(), totalSkuStock);
                                throw new AdminServiceException(ExceptionDefinition.ORDER_SKU_STOCK_NOT_ENOUGH_V2);
                            }
                        }
                    }
                    //更新仓库-商品操作
                    WarehouseOperLogDO warehouseOperLogDO = new WarehouseOperLogDO();
                    warehouseOperLogDO.setGmtCreate(now);
                    warehouseOperLogDO.setGmtUpdate(now);
                    warehouseOperLogDO.setChangeSkuStock(salesSendOrderGoodsDO.getSendGoodsStock());
                    warehouseOperLogDO.setAdminId(adminId);
                    warehouseOperLogDO.setSkuId(salesSendOrderGoodsDO.getSkuId());
                    warehouseOperLogDO.setType(2);//2代表出库
                    warehouseOperLogDO.setChangeSkuStock(changeSkuStock);
                    warehouseOperLogDO.setWarehouseSkuStock(totalSkuStock);
                    warehouseOperLogDO.setWarehouseId(salesSendOrderDO.getWarehouseId());
                    warehouseOperLogDO.setWarehouseName(warehouseDO.getWarehouseName());
                    warehouseOperLogDO.setSkuName(salesSendOrderGoodsDO.getSkuName());
                    warehouseOperLogDO.setSpuName(salesSendOrderGoodsDO.getSpuName());
                    warehouseOperLogDO.setSpuId(salesSendOrderGoodsDO.getGoodsId());
                    warehouseOperLogDO.setSkuId(salesSendOrderGoodsDO.getSkuId());
                    warehouseOperLogDO.setSkuNo(salesSendOrderGoodsDO.getGoodsBarCode());
                    warehouseOperLogDO.setOrderNo(salesSendOrderDO.getSendOrderNo());
                    warehouseOperLogDO.setOrderId(salesSendOrderDO.getId());
                    warehouseOperLogDO.setAdminName(adminDTO.getRealname());
                    warehouseOperLogDO.setCurrAvgPrice(warehouseGoodsDO.getPrice().intValue());
                    warehouseOperLogDO.setOrigSkuStock(origWarehouseGoodsDO.getWarehouseSkuStock());
                    warehouseOperLogDO.setOrigAvgPrice(origWarehouseGoodsDO.getPrice().intValue());
                    warehouseOperLogMapper.insert(warehouseOperLogDO);

                }
                //插入应收记录
                AccountsReceivableDO accountsReceivableDO = new AccountsReceivableDO();
                accountsReceivableDO.setGmtUpdate(now);
                accountsReceivableDO.setAdminId(adminId);
                accountsReceivableDO.setAdminName(adminDTO.getRealname());
                accountsReceivableDO.setFinishAmount(0);
                accountsReceivableDO.setGmtCreate(now);
                accountsReceivableDO.setSendOrderNo(salesSendOrderDO.getSendOrderNo());
                accountsReceivableDO.setSendOrderId(salesSendOrderDO.getId());
                accountsReceivableDO.setCustomerId(salesSendOrderDO.getStoreId());
                accountsReceivableDO.setReceivableNo("");
                accountsReceivableDO.setCustomerName(salesSendOrderDO.getStoreName());
                accountsReceivableDO.setReceivableAllAmount(salesSendOrderDO.getSendOrderGoodsPrice());
                accountsReceivableDO.setReceivableYouhuiAmount(salesSendOrderDO.getSendOrderConpon());
                accountsReceivableDO.setReceivableAmount(salesSendOrderDO.getSendOrderPrice());
                accountsReceivableDO.setWarehouseId(salesSendOrderDO.getWarehouseId());
                accountsReceivableMapper.insert(accountsReceivableDO);
            }
            //插入操作表
            //操作人真实姓名
            if (adminDTO != null) {
                insertSendOrderOperLog(salesSendOrderDO, adminDTO);
            }
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }


}